package com.yanqu.road.dao.impl.message;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.enums.eMessageSubscribeType;
import com.yanqu.road.entity.messageSubscribe.entity.MessageSubscribeInfo;
import com.yanqu.road.entity.messageSubscribe.entity.MessageUserNoticeData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MessageNoticeDaoImpl extends TempDataObjectDao<MessageUserNoticeData> {

    public MessageUserNoticeData getMessageUserNotice(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_message_notice where userId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                MessageUserNoticeData data = parse(rs);
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public Map<Long, MessageUserNoticeData> getMessageUserNoticeMap(long lastUpdateTime) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, MessageUserNoticeData> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_u_message_notice WHERE lastUpdateTime > ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++, lastUpdateTime);
            rs = call.executeQuery();
            while (rs.next()) {
                MessageUserNoticeData data = parse(rs);
                dataMap.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(MessageUserNoticeData data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_message_notice" +
                "(userId,sonTime,mistressTime,hospitalTime,travelTime,patronsWarTime,patronsWarSilverTime," +
                "noonHuntTime,nightHuntTime,clinicTime,bussinessPosRobTime,manorFoodTime,manorDragonTime,wineShopTime,lastUpdateTime,subTimeMap) " +
                "values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setLong(i++, data.getSonTime());
            call.setLong(i++, data.getMistressTime());
            call.setLong(i++, data.getHospitalTime());
            call.setLong(i++, data.getTravelTime());
            call.setLong(i++, data.getPatronsWarTime());
            call.setLong(i++, data.getPatronsWarSilverTime());
            call.setLong(i++, data.getNoonHuntTime());
            call.setLong(i++, data.getNightHuntTime());
            call.setLong(i++, data.getClinicTime());
            call.setLong(i++, data.getBussinessPosRobTime());
            call.setLong(i++, data.getManorFoodTime());
            call.setLong(i++, data.getManorDragonTime());
            call.setLong(i++, data.getWineShopTime());
            call.setLong(i++, data.getLastUpdateTime());
            call.setString(i++, JSON.toJSONString(data.getSubTimeMap()));
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(MessageUserNoticeData data) {
        PreparedStatement call = null;
        String sql = "update t_u_message_notice set " +
                " sonTime=?,mistressTime=?,hospitalTime=?,travelTime=?,patronsWarTime=?," +
                " patronsWarSilverTime=?,noonHuntTime=?,nightHuntTime=?,clinicTime=?,bussinessPosRobTime=?," +
                " manorFoodTime=?,manorDragonTime=?,wineShopTime=?,lastUpdateTime=?,subTimeMap=? " +
                " where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getSonTime());
            call.setLong(i++, data.getMistressTime());
            call.setLong(i++, data.getHospitalTime());
            call.setLong(i++, data.getTravelTime());
            call.setLong(i++, data.getPatronsWarTime());
            call.setLong(i++, data.getPatronsWarSilverTime());
            call.setLong(i++, data.getNoonHuntTime());
            call.setLong(i++, data.getNightHuntTime());
            call.setLong(i++, data.getClinicTime());
            call.setLong(i++, data.getBussinessPosRobTime());
            call.setLong(i++, data.getManorFoodTime());
            call.setLong(i++, data.getManorDragonTime());
            call.setLong(i++, data.getWineShopTime());
            call.setLong(i++, data.getLastUpdateTime());
            call.setString(i++, JSON.toJSONString(data.getSubTimeMap()));
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected MessageUserNoticeData parseData(ResultSet rs) throws SQLException {
        MessageUserNoticeData data = new MessageUserNoticeData();
        data.setUserId(rs.getLong("userId"));
        data.setSonTime(rs.getLong("sonTime"));
        data.setMistressTime(rs.getLong("mistressTime"));
        data.setHospitalTime(rs.getLong("hospitalTime"));
        data.setTravelTime(rs.getLong("travelTime"));
        data.setPatronsWarTime(rs.getLong("patronsWarTime"));
        data.setPatronsWarSilverTime(rs.getLong("patronsWarSilverTime"));
        data.setNightHuntTime(rs.getLong("nightHuntTime"));
        data.setClinicTime(rs.getLong("clinicTime"));
        data.setBussinessPosRobTime(rs.getLong("bussinessPosRobTime"));
        data.setManorFoodTime(rs.getLong("manorFoodTime"));
        data.setManorDragonTime(rs.getLong("manorDragonTime"));
        data.setWineShopTime(rs.getLong("wineShopTime"));
        data.setLastUpdateTime(rs.getLong("lastUpdateTime"));
        String str = rs.getString("subTimeMap");
        if (StringUtils.isNullOrEmpty(str)) {
            data.setSubTimeMap(new ConcurrentHashMap<>());
        }else {
            data.setSubTimeMap(JSON.parseObject(str, new TypeReference<Map<eMessageSubscribeType, List<Long>>>() {
            }));
        }
        return data;
    }
}
